package Airport;

import java.util.HashMap;
import java.util.Map;

public class Airport {

	private static Airport instance = null;
	public static int TimeTicks;

	private final int NUMBER_OF_RUNWAYS;
	private final int MAX_CAPACITY;
	private final int LANDING_AND_TAKEOFF_TIME;

	private Map<Integer, Integer> closedRunwaysUntil = new HashMap<Integer, Integer>();
	private Map<Integer, Integer> planesOnRunways = new HashMap<Integer, Integer>();
	private int[][] runways;
	private int currentCapacity = 0;

	private Airport(int numberOfRunways, int capacity, int time) {

		NUMBER_OF_RUNWAYS = numberOfRunways;
		MAX_CAPACITY = capacity;
		LANDING_AND_TAKEOFF_TIME = time;

		runways = new int[NUMBER_OF_RUNWAYS][MAX_CAPACITY / NUMBER_OF_RUNWAYS];

	}

	public static Airport getInstance(int tracks, int capacity, int time) {

		if (instance == null && (tracks > 0 && tracks < 100)
				&& (capacity > 0 && capacity < 100) && (time > 0 && time < 100)) {
			instance = new Airport(tracks, capacity, time);

		}

		return instance;
	}

	public String requestLanding(int plane, int fuel) {

		if (fuel < LANDING_AND_TAKEOFF_TIME || currentCapacity >= MAX_CAPACITY) {

			return String.valueOf(plane) + " is redirected.";

		} else {

			for (int i = 0; i < runways.length; i++) {

				for (int j = 0; j < runways[i].length; j++) {

					if (runways[i][j] == 0) {

						if (closedRunwaysUntil.containsKey(i)) {

							if (closedRunwaysUntil.get(i) < TimeTicks) {

								runways[i][j] = plane;
								planesOnRunways.put(plane, i);
								currentCapacity++;

								return String.valueOf(plane)
										+ " lands on runway number "
										+ String.valueOf(i);
							}
						} else {

							runways[i][j] = plane;
							planesOnRunways.put(plane, i);
							currentCapacity++;

							return String.valueOf(plane)
									+ " lands on runway number "
									+ String.valueOf(i);
						}
					}
				}
			}

			return String.valueOf(plane) + " is redirected.";
		}
	}

	public String requestTakeoff(int plane) {

		if (planesOnRunways.containsKey(plane) == false) {
			return "There is no such plane here.";
		}

		int i = planesOnRunways.get(plane);

		if (closedRunwaysUntil.containsKey(i)) {

			if (closedRunwaysUntil.get(i) < TimeTicks) {

				for (int j = 0; j < runways[i].length; j++) {

					if (runways[i][j] == plane) {

						runways[i][j] = 0;

						planesOnRunways.remove(plane);

						return plane + " has taken off runway " + i;

					}
				}

				return "404 Plane not found";

			} else {
				return plane + "cannot take off now. The runway is closed.";
			}
		} else {

			for (int j = 0; j < runways[i].length; j++) {

				if (runways[i][j] == plane) {

					runways[i][j] = 0;

					planesOnRunways.remove(plane);

					return plane + " has taken off runway " + i;
				}
			}

			return "404 Plane not found";
		}
	}

	public String closeRunway(int runway, int time) {

		closedRunwaysUntil.put(runway, TimeTicks + time);

		return runway + " runway is closed off for " + time;

	}

}
